#pragma once
#include <cmath>
#include <string>
#define PI 3.14159265358979323846 // pi
#include "electric_bases.h"
class single_phase_voltage_source : public electric_base
{
public:
    double simulation_time;
    double init_phase = 0.0; // in rad
    double f = 50;

    double r = 1.0;
    double g = 1.0 / r;
    enum class type
    {
        AC,
        DC
    };
    type vtype = type::AC;

    double Vamp = 5.0;
    double Vsource;
    double Isource;

    void set(param Params)
    {
        Vamp = Params["Vamp"];
        r = Params["r"];
        g = 1.0 / r;
        f = Params["freq"];
        init_phase = Params["init_phase"];
        vtype = (int)Params["vtype"] == 0 ? type::AC : type::DC;
    }

    void set(double _Vamp, double _r, double _f, double _ip, std::string _vtype)
    {
        Vamp = _Vamp;
        r = _r;
        g = 1.0 / r;
        f = _f;
        init_phase = _ip;
        if (_vtype == "AC")
            vtype = type::AC;
        else if (_vtype == "DC")
            vtype = type::DC;
    }

    void control(double t)
    {
        simulation_time = t;
        switch (vtype)
        {
        case type::AC:
            Vsource = Vamp * sqrt(2) * sin(2 * PI * f * t + init_phase);
            break;
        case type::DC:
            Vsource = Vamp;
            break;
        }
        Isource = Vsource * g;
    }
};